package DataBase;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DataSearch extends JInternalFrame {
	Connection conn;
//	Updata update;
	

	DatabaseTest main;
	DefaultTableModel model;
	private JPanel panel;
	private JTextField search;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					DataSearch frame = new DataSearch();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public DataSearch(Connection conn, DatabaseTest dt) {
		super("조회", true, true, true, false);
		this.conn = conn;
		this.main = dt;
		setBounds(700, 0, 590, 450);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getSearch());
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getSearch() {
		if (search == null) {
			search = new JTextField();
			search.setColumns(10);
		}
		return search;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리 실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				setList();	
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					if( main.Modify == null){
						main.openModify();
					}
					int row = table.getSelectedRow();
					String s = String.valueOf(table.getValueAt(row, 3));
					String c = (String)table.getValueAt(row, 1);
					String e = String.valueOf(table.getValueAt(row, 2));
					
					main.Modify.setData(s, c, e);
					main.Modify.setVisible(true);
					main.Modify.toFront();
				}
			});
		}
		return table;
	}
	
	public void setList(){
		String s = search.getText();
		try{
			PreparedStatement pstmt = conn.prepareStatement(s);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector();
			v.add("No");
			for(int i=1; i<=rsm.getColumnCount(); i++){
				v.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(v,0);
			table.setModel(model);
			
			
			
			int cnt = 1;
			while(rs.next()){
				Vector d = new Vector<>();
				d.add(cnt);
				for(int i=1; i<=rsm.getColumnCount(); i++){
					d.add(rs.getString(i));
				}
				model.addRow(d);
				cnt++;
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, "잘못 된 SQL문장입니다.");
		}
	}

}
